package com.esd.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.esd.crm.Constants;
import com.esd.crm.entity.Code;
import com.esd.crm.service.CodeService;

@Controller
@RequestMapping("/security")
public class CodeController {

	private static final Logger logger = LogManager.getLogger(CodeController.class);

	@Autowired
	private CodeService codeService;

	/**
	 * 跳转添加产品页
	 */
	@RequestMapping(value = "/addCode", method = RequestMethod.GET)
	public ModelAndView addProductGet(HttpSession session, HttpServletRequest request) {
		logger.info("跳转添加标准产品页");
		return new ModelAndView("code/code_add");
	}

	/**
	 * 添加产品
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addCode", method = RequestMethod.POST)
	public ModelAndView addProductPost(HttpSession session, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		logger.info("添加标准产品请求");
		Integer userid = Integer.valueOf(session.getAttribute(Constants.USER_ID).toString());

		String codeKey = request.getParameter("codeKey");
		String codeValue = request.getParameter("codeValue");
		Code c = new Code();
		c.setCodeKey(codeKey);
		c.setCodeValue(codeValue);
		c.setCreatetime(new Date());
		c.setCreateuser(userid);
		codeService.insert(c);
		// 同步更新合计
		return new ModelAndView("redirect:/security/successAndClose");
	}

	@RequestMapping(value = "/code", method = RequestMethod.GET)
	public ModelAndView productGet(HttpSession session, HttpServletRequest request) {
		return new ModelAndView("code/code");
	}

	@RequestMapping(value = "/code", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> productPost(HttpSession session, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		int totle = 1;
		List<Code> list = null;
		int page = Integer.valueOf(request.getParameter("page"));
		param.put("page", (page - 1) * 20);
		param.put("totle", 20);
		// 获取查询参数
		String codeKey = request.getParameter("codeKey");
		String codeValue = request.getParameter("codeValue");
		// // 组合查询参数
		if (StringUtils.isNotBlank(codeKey)) {
			param.put("codeKey", codeKey);
		}
		if (StringUtils.isNotBlank(codeValue)) {
			param.put("codeValue", codeValue);
		}
		list = codeService.selectByParam(param);
		totle = codeService.selectByParamCount(param);
		int u = totle % 20;
		int pageCount = 1;
		if (u > 0) {
			pageCount = totle / 20 + 1;
		} else {
			pageCount = totle / 20;
		}
		map.put(Constants.LIST, list);
		map.put(Constants.TOTLE, totle);
		map.put(Constants.TOTLE_PAGE, pageCount);
		return map;
	}

	/**
	 * 跳转添加产品页
	 */
	@RequestMapping(value = "/updateCode", method = RequestMethod.GET)
	public ModelAndView updateProductGet(HttpSession session, HttpServletRequest request) {
		logger.info("跳转更新选项页");
		String id = request.getParameter("id");
		Code c = codeService.selectByPrimaryKey(Integer.valueOf(id));
		ModelAndView mv = new ModelAndView("code/code_update");
		mv.addObject("id", c.getId());
		mv.addObject("codeKey", c.getCodeKey());
		mv.addObject("codeValue", c.getCodeValue());
		return mv;
	}

	/**
	 * 更新产品
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateCode", method = RequestMethod.POST)
	public ModelAndView updateProductPost(HttpSession session, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		Integer userid = Integer.valueOf(session.getAttribute(Constants.USER_ID).toString());
		logger.info("更新选项页");
		String id = request.getParameter("id");
		Code c = codeService.selectByPrimaryKey(Integer.valueOf(id));
		String codeKey = request.getParameter("codeKey");
		String codeValue = request.getParameter("codeValue");
		c.setCodeKey(codeKey);
		c.setCodeValue(codeValue);
		c.setCreatetime(new Date());
		c.setCreateuser(userid);
		codeService.updateByPrimaryKeySelective(c);
		return new ModelAndView("redirect:/security/successAndClose");
	}

	/**
	 * 跳转添加产品页
	 */
	@RequestMapping(value = "/deleteCode", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteProductPost(HttpSession session, HttpServletRequest request) {
		logger.info("删除选项");
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		codeService.deleteByPrimaryKey(Integer.valueOf(id));
		map.put("message", "删除成功!");
		return map;
	}

	/**
	 * 跳转添加产品页
	 */
	@RequestMapping(value = "/codeSelectProduct", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> codeSelectProductPost(HttpSession session, HttpServletRequest request) {
		logger.info("查询产品选项数据名称");
		Map<String, Object> map = new HashMap<String, Object>();
		String codeKey = request.getParameter("codeKey");
		List<Code> list = codeService.selectCodeProduct(codeKey);
		map.put("list", list);
		return map;
	}
}
